import yaml
import os

def read_yaml(file_path):
    """
    读取 YAML 文件并将其内容转换为 Python 字典。
    param file_path: YAML 文件的路径
    """
    try:
        # 检查文件是否存在
        if not os.path.exists(file_path):
            raise FileNotFoundError(f"文件 {file_path} 不存在")
        # 打开并读取 YAML 文件
        with open(file_path, "r", encoding="utf-8") as file:
            data = yaml.safe_load(file)  # 使用 safe_load 避免潜在的安全风险
            return data
    except yaml.YAMLError as e:
        print(f"YAML 文件解析错误: {e}")
    except Exception as e:
        print(f"读取文件时发生错误: {e}")


if __name__ == "__main__":
    # 调试代码
    yaml_file_path = r"data\test.yaml"
    config_data = read_yaml(yaml_file_path)
    if config_data:
        print("读取到的 YAML 数据:", config_data)